package com.lxd.milktea.config;

import com.lxd.milktea.interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ProjectName: milktea
 * @Package: com.lxd.milktea.config
 * @ClassName: webconfig
 * @Author: carrymaniac
 * @Description:
 * @Date: 2020/5/19 5:00 下午
 * @Version:
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    UserInterceptor userInterceptor(){
        return new UserInterceptor();
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        //登陆拦截器排除URL
//        registry.addInterceptor(userInterceptor())
//                .excludePathPatterns("/static/**","/user/login","/index.html")
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
//        ;
//    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    /**
     * 跨域使用
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }
}
